package spring.mvc.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.domain.Comment;
import spring.mvc.service.CommentService;

@Controller
public class CommentController {
	@Resource
	private CommentService CommentService;
	
	/* no_ajax
	 * 
	@RequestMapping(value = "/co_write", method = RequestMethod.POST)
	public String co_write(@ModelAttribute("comment") Comment comment) throws Exception
	{
		Integer seq = CommentService.co_write(comment);
		return "redirect:/view?seq="+seq;
	}
	@RequestMapping(value = "/co_update_com", method = RequestMethod.POST)
	public String co_delete (@ModelAttribute("comment") Comment comment) throws Exception
	{	
		CommentService.co_delete(comment);
		return "redirect:/view?seq="+comment.getSeq();
	}
	
	@RequestMapping(value = "/co_update_com", method = RequestMethod.POST)
	public String co_update_com (@ModelAttribute("comment") Comment comment) throws Exception
	{	
		CommentService.co_update_com(comment);
		return "redirect:/view?seq="+comment.getSeq();
	}*/
	
	//json으로 받은 후 ajax로 출력
	@RequestMapping(value = "/co_write", method = RequestMethod.POST)
	public @ResponseBody List<Comment> co_write(@ModelAttribute("comment") Comment comment) throws Exception
	{
		CommentService.co_write(comment);
		return CommentService.list(Integer.toString(comment.getSeq()));
	}
	
	@RequestMapping(value = "/co_delete", method = RequestMethod.POST)
	public @ResponseBody List<Comment> co_delete (@ModelAttribute("comment") Comment comment) throws Exception
	{	
		CommentService.co_delete(comment);
		return CommentService.list(Integer.toString(comment.getSeq()));
	}
	
	@RequestMapping(value = "/co_update_com", method = RequestMethod.POST)
	public @ResponseBody List<Comment>co_update_com (@ModelAttribute("comment") Comment comment, Model model) throws Exception
	{	
		CommentService.co_update_com(comment);
		return CommentService.list(Integer.toString(comment.getSeq()));
	}
}
